﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AppManager :Step
{
    private static AppManager mInstance = null;
    public static AppManager Instance
    {
        get
        {
            if (mInstance == null)
            {
                mInstance = new AppManager();
            }
            return mInstance;
        }
    }


    private Step taskCtrl = null;
    private Step settingCtrl = null;
    private Step logCtrl = null;

    private GameObject mainPanel = null;
    private string prefabPath = "Prefabs/mainPanel";

    private UIButton startBtn = null;
    private UIButton settingBtn = null;
    private UIButton logBtn = null;
    private UIButton exitBtn = null;


    public override void Init()
    {
        if (mainPanel == null)
        {
            var go = Resources.Load(prefabPath) as GameObject;
            mainPanel = GameObject.Instantiate<GameObject>(go);
            var trans = mainPanel.transform;
            trans.SetParent(NGUITools.TryGetUIRoot().transform);
            trans.localScale = Vector3.one;
            trans.localPosition = Vector3.one;
            trans.localEulerAngles = Vector3.zero;

            InitPanel();
        }

        if (taskCtrl == null)
        {
            taskCtrl = new TaskManager();
        }

        if (settingCtrl == null)
        {
            settingCtrl = new SettingCtrl();
        }

        if (logCtrl == null)
        {
             logCtrl = new LogCtrl();
        }

        if (!mainPanel.activeSelf)
        {
            mainPanel.SetActive(true);
        }
    }

    private void InitPanel()
    {
        var trans = mainPanel.transform;
        startBtn = trans.Find("startBtn").GetComponent<UIButton>();
        settingBtn = trans.Find("settingBtn").GetComponent<UIButton>();
        logBtn = trans.Find("logBtn").GetComponent<UIButton>();
        exitBtn = trans.Find("exitBtn").GetComponent<UIButton>();

        startBtn.onClick.Add(new EventDelegate(OnClickStart));
        settingBtn.onClick.Add(new EventDelegate(OnClickSetting));
        logBtn.onClick.Add(new EventDelegate(OnClickLog));
        exitBtn.onClick.Add(new EventDelegate(OnClickExit));

        startBtn.transform.Find("Label").GetComponent<UILabel>().text = "开始";
        settingBtn.transform.Find("Label").GetComponent<UILabel>().text = "设置";
        logBtn.transform.Find("Label").GetComponent<UILabel>().text = "查看";
        exitBtn.transform.Find("Label").GetComponent<UILabel>().text = "退出";
    }



    void OnClickStart()
    {
        this.Exit();
        taskCtrl.Enter(this);
    }

    void OnClickSetting()
    {
        this.Exit();
        settingCtrl.Enter(this);
    }

    void OnClickLog()
    {
        this.Exit();
        logCtrl.Enter(this);
    }

    void OnClickExit()
    {
        NoticeCtrl.Instance.Open("你确定要退出程序吗？", (bool isYes) =>
        {
            if (isYes)
            {
                this.Exit();
                Application.Quit();
            }
        });
    }


    public override void BeforeExit()
    {
        mainPanel.SetActive(false);
    }
}


